package com.ruoyi.reservationserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.reservationserver.dao.UserDao;
import com.ruoyi.reservationserver.entity.User;
import com.ruoyi.reservationserver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户表(User)表服务实现类
 *
 * @author makejava
 * @since 2023-07-04 16:21:50
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao,User> implements UserService {
    @Resource
    private UserDao userDao;

    @Resource
    private UserService userService;

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;


    @Override
    public User login(String account) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wq ->{
            wq.eq("account",account)
                    .or()
                    .eq("email",account)
                    .or()
                    .eq("phone_number",account);
        });
//        }).eq("password",password);

        User user = userDao.selectOne(queryWrapper);
        return user;
    }

    /**
     * 通过ID查询单条数据
     *
     *
     * @return 实例对象
     */
    @Override
    public PageInfo<User> selectUserWithPage(Integer page, Integer count) {
        PageHelper.startPage(page, count);

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("user_id");
        List<User> usersList = userService.list(queryWrapper);

        PageInfo<User> pageInfo=new PageInfo<>(usersList);
        return pageInfo;
    }

    @Override
    public User queryById(Integer userId) {
        return this.userDao.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<User> queryByPage(User user, PageRequest pageRequest) {
        long total = this.userDao.count(user);
        return new PageImpl<>(this.userDao.queryAllByLimit(user, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.userDao.deleteById(userId) > 0;
    }



}
